#!/bin/bash

echo "DANGER: You should probably log into all the machines and run 'shutdown -h now' to prevent filesystem corruption before running this"

# shut down the router (system_powerdown doesn't seem to work)
# These commands are killing the qemu process, but not the wrapper around them
sudo /bin/bash -c "echo 'quit' | socat - unix-connect:/tmp/router-monitor.sock >& /dev/null"
sudo /bin/bash -c "echo 'quit' | socat - unix-connect:/tmp/vm1-monitor.sock >& /dev/null"
sudo /bin/bash -c "echo 'quit' | socat - unix-connect:/tmp/vm2-monitor.sock >& /dev/null"

# Obviously this is bad, but it's a temporary fix while playing around in the
# "I'm the only one here" sandbox
sudo /usr/bin/killall vdeqemu

# remove the switch
sudo /bin/kill -s SIGTERM $(cat /tmp/vdepid1)
sudo /bin/kill -s SIGTERM $(cat /tmp/vdepid2)

# remove the tap device
sudo /usr/sbin/tunctl -d divirt0

# remove the filsystems
# To save time we're not doing this

# Notes:
#   * 'quit' doesn't seem to be killing things all the time, 
#     might need to improve on this.
